<?php

/**
* 获得服务器上的 GD 版本
 *
 * @access      public
 * @return      int         可能的值为0，1，2
 */
function gd_version(){
	static $version = -1;

	if ($version >= 0){
		return $version;
	}

	if (!extension_loaded('gd')){
		$version = 0;
	}
	else{
		// 尝试使用gd_info函数
		if (PHP_VERSION >= '4.3'){
			if (function_exists('gd_info')){
				$ver_info = gd_info();
				preg_match('/\d/', $ver_info['GD Version'], $match);
				$version = $match[0];
			}
			else{
				if (function_exists('imagecreatetruecolor')){
					$version = 2;
				}
				elseif (function_exists('imagecreate')){
					$version = 1;
				}
			}
		}
		else{
			if (preg_match('/phpinfo/', ini_get('disable_functions'))){
				/* 如果phpinfo被禁用，无法确定gd版本 */
				$version = 1;
			}
			else{
			  // 使用phpinfo函数
			   ob_start();
			   phpinfo(8);
			   $info = ob_get_contents();
			   ob_end_clean();
			   $info = stristr($info, 'gd version');
			   preg_match('/\d/', $info, $match);
			   $version = $match[0];
			}
		 }
	}
	return $version;
}
 
/**
 * 获得当前格林威治时间的时间戳
 *
 * @return  integer
 */
function gmtime(){
    return time();
}

/**
 *  生成一个用户自定义时区日期的GMT时间戳
 *
 * @access  public
 * @param   int     $hour
 * @param   int     $minute
 * @param   int     $second
 * @param   int     $month
 * @param   int     $day
 * @param   int     $year
 *
 * @return void
 */
function local_mktime($hour = NULL , $minute= NULL, $second = NULL,  $month = NULL,  $day = NULL,  $year = NULL){
    $time 	= 	mktime($hour, $minute, $second, $month, $day, $year);
    return $time;
}

/**
 * 过滤用户输入的基本数据，防止script攻击
 *
 * @access      public
 * @return      string
 */
function compile_str($str){
    $arr 	= 	array('<' => '＜', '>' => '＞');
    return strtr($str, $arr);
}


/**
 * 截取UTF-8编码下字符串的函数
 *
 * @param   string      $str        被截取的字符串
 * @param   int         $length     截取的长度
 * @param   bool        $append     是否附加省略号
 *
 * @return  string
 */
function sub_str($str, $length = 0, $append = true){
    $str = trim($str);
    $strlength = strlen($str);

    if ($length == 0 || $length >= $strlength){
        return $str;
    }
    elseif ($length < 0){
        $length = $strlength + $length;
        if ($length < 0){
            $length = $strlength;
        }
    }

    if (function_exists('mb_substr')){
        $newstr = mb_substr($str, 0, $length, EC_CHARSET);
    }
    elseif (function_exists('iconv_substr')){
        $newstr = iconv_substr($str, 0, $length, EC_CHARSET);
    }
    else{
        //$newstr = trim_right(substr($str, 0, $length));
        $newstr = substr($str, 0, $length);
    }

    if ($append && $str != $newstr){
        $newstr .= '...';
    }

    return $newstr;
}

/**
 * 自动编译路径
 * 处理多级position
 * 
 */
function build_url(){
	// controller
	$array	=	array(
		'service'=>'售后服务',
		'index'=>'',
		'download'=>'下载中心',
		'firm'=>'关于音伴尔'
	);
	// action
	$action_array	=	array(
		'goods'=>array(
			'index'		=> '产品展示',
			'list'		=> '所有产品',
			'wifiyx'	=> '音箱展示',
			'mini'		=> '迷你音箱',
			'upan'		=> '超大U盘',
		),
		'download'=>array(
			'app'		=> '',
		),
		'service'=>array(),
		'article'=>array(),
		'firm'=>array()
	);
	
	//处理一级	
	$position	=	array();
	$url_array	=	$_REQUEST['_URL_'];
	// controller
	if(empty($url_array[0])) 
		$controller = 'index';
	else 
		$controller = $url_array[0];
	// action
	if(empty($url_array[1])){
		$action =	'index';
	}
	else{
		$action = $url_array[1];
	}	
	
	$url		=	$controller.'/'.$action;
	$one_url 	= 	U($url);		
	$one_name	=	$array[$controller];		
	$position['one_url']	=	$one_url;
	$position['one_name']	=	$one_name;
	// 处理二级	
	$two_name	=	'';
	$two_url	=	'';
	switch($controller){
		case 'index':
			break;
		case 'goods':
			$two_name	=	$action_array['goods'][$action];
			break;
		case 'service':
			$two_name	=	'服务政策';
			break;
		case 'article':
			$two_name	=	'相关文章';
			break;
		case 'download':
			// add code here
			break;
		case 'firm':
			// add code here
			$two_name	=	'公司简介';
			break;
		default:
			break;
	}
	
	$position['two_name']	=	$two_name;
	$position['two_url']	=	$two_url;
	return $position;	
}

/**
 * 自动生成前一步的访问路径
 * 
 */
function auto_create_back_url(){	
	$_host_name		=	$_SERVER['HTTP_HOST'];
	$_controller	=	$_GET['_URL_'][0];
	$_action		=	$_GET['_URL_'][1];
	
	$_url			=	U($_controller . '/' . $_action);
	
	$_COOKIE['back_url']	=	$_url;
	return $url;
}

/**
 * 调用UCenter的函数
 *
 * @param   string  $func
 * @param   array   $params
 *
 * @return  mixed
 */
function uc_call($func, $params=null){
    restore_error_handler();
    if (!function_exists($func)){
        include_once UC_CLIENT_PATH . 'client/' . 'client.php';
    }   
    $res = call_user_func_array($func, $params); 
    set_error_handler('exception_handler');
    return $res;
}

/**
 * 系统自动编译提示页面信息
 * 
 */
function build_tips_msg($msg_type,$params = array()){		
	$img_list				=	array(
		'qs_unpayed'		=>	'icon-success.png',
		'qs_paying'			=>	'icon-success.png',
		'qs_payed'			=>	'icon-success.png',
		'qs_unconfirmed'	=>	'icon-success.png',
		'qs_confirming' 	=>	'icon-success.png',
		'qs_comfirmed' 		=>	'icon-success.png',
		'qs_tips_success' 	=>	'icon-success.png',
	);
	//default settings
	$msg['first_url']   	= "http://www.yinbaner.com";
	$msg['first_url_text']  = "返回首页";
	$msg['second_url']  	= U('goods/index');
	$msg['second_url_text'] = "继续购物";
	$msg['tips_text'] 		= "购物车为空,去商城看看呗 ^_^";
	$msg['is_auto_jump'] 	= 0;
	$msg['jump_content'] 	= "";
	$msg['tips_type'] 		= $msg_type;	

	switch($msg_type){		
		case QS_UNPAYED:
			$msg['tips_text'] 	= 	"订单未支付...";
			$msg['img_url']		=	$img_list['qs_unpayed'];
			//订单ID
			$order_id			=	intval($_REQUEST['order_id']);			
			$msg['second_url']  	= 	U('flow/payorder/') . "?id=" . $order_id;
			$msg['second_url_text'] =	"我要支付";
			break;
		case QS_PAYING:
			$msg['tips_text'] 	= 	"订单正在支付中...";
			$msg['img_url']		=	$img_list['qs_paying'];
			$msg['second_url']  	= 	U('user/order_list');
			$msg['second_url_text'] =	"去看看我的订单";	
			break;
		case QS_PAYED:
			$msg['tips_text'] 	= 	"订单已支付,可以去我的订单看看的哦  ^_^";
			$msg['img_url']		=	$img_list['qs_payed'];
			break;
		case QS_PAY_SUCCESS:
    		/*是否跳转*/
			$msg['is_auto_jump'] 	= 1;
			$msg['jump_content'] 	= '<meta http-equiv="Refresh" content="5;url=' . U('user/order_list') . '" />';			
			$msg['tips_text'] 		= 	"订单支付成功，即将为您跳转到订单中心哦~~^_^";
			$msg['img_url']			=	$img_list['qs_payed'];
			$msg['second_url']  	= 	U('goods/index');
			$msg['second_url_text'] =	"继续购物";			
			break;
		case QS_UNCONFIRMED:
			$msg['tips_text'] 	= 	"订单未确认...";
			$msg['img_url']		=	$img_list['qs_unconfirmed'];
			break;
		case QS_CONFIRMING:
			$msg['tips_text'] 	= 	"订单确认中...";
			$msg['img_url']		=	$img_list['qs_confirming'];
			break;
		case QS_CONFIRMED:
			$msg['tips_text'] 	= 	"订单已经确认";
			$msg['img_url']		=	$img_list['qs_confirmed'];
			break;
		case QS_CONFIRM_SUCCESS:
			$msg['tips_text'] 	= 	"订单已经确认成功";
			$msg['img_url']		=	$img_list['qs_confirmed'];
			break;
		case QS_ORDER_EMPTY:
			$msg['tips_text'] 	= 	"订单不存在,请联系客服查明原因";
			$msg['img_url']		=	$img_list['qs_confirmed'];
			break;
		case QS_GENERAL_TIPS:
			$msg				=	array_merge($msg,$params);
			$msg['img_url'] 	= 	$img_list['qs_tips_success'];
			break;
		default:
			break;
	}
	return $msg;
}

















 
